package com.cxg.fairy.ec.main.personal.address;

import android.graphics.Color;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.ui.widget.CheckBox.CheckBoxGType;
import com.cxg.fairy.util.log.LatteLogger;
import com.diabin.latte.ec.R;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.MultipleRecyclerAdapter;
import com.cxg.fairy.ui.recycler.MultipleViewHolder;

import java.util.List;

/**
 * Created by CFairy
 */

public class AddressAdapter extends MultipleRecyclerAdapter {

    protected AddressAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(AddressItemType.ITEM_ADDRESS, R.layout.item_address);
    }

    @Override
    protected void convert(final MultipleViewHolder holder, final MultipleItemEntity entity) {
        super.convert(holder, entity);

        switch (holder.getItemViewType()) {
            case AddressItemType.ITEM_ADDRESS:
                final String name = entity.getField(MultipleFields.NAME);
                final String phone = entity.getField(AddressItemFields.PHONE);
                final String address = entity.getField(AddressItemFields.ADDRESS);
                final boolean isDefault = entity.getField(MultipleFields.TAG);
                final int id = entity.getField(MultipleFields.ID);

                final AppCompatTextView nameText = holder.getView(R.id.tv_address_name);
                final AppCompatTextView phoneText = holder.getView(R.id.tv_address_phone);
                final AppCompatTextView addressText = holder.getView(R.id.tv_address_address);
                final AppCompatButton deleteButton = holder.getView(R.id.btn_address_delete);
                final AppCompatTextView defaultText = holder.getView(R.id.atv_default_text);

                //delete
                deleteButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RestClient.builder()
                                .url("deleteAddressById")
                                .params("uuid", AccountManager.getSignUUID())
                                .params("addressId", id)
                                .success(new ISuccess() {
                                    @Override
                                    public void onSuccess(String response) {
                                        final List<MultipleItemEntity> delData = new AddressDataConverter().setJsonData(response).convert();
                                        mData.clear();
                                        mData.addAll(delData);
                                        notifyDataSetChanged();
                                    }
                                })
                                .build()
                                .post();
                    }
                });

                //set default
                final CheckBoxGType mcheckBoxG = holder.getView(R.id.checkBoxG);
                if (isDefault) {
                    mcheckBoxG.setChecked(true);
                    defaultText.setText(R.string.address_default);
                    defaultText.setTextColor(Color.BLUE);
                } else{
                    mcheckBoxG.setChecked(false);
                    defaultText.setText(R.string.address_set_default);
                    defaultText.setTextColor(Color.parseColor("#303030"));
                }

                mcheckBoxG.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mcheckBoxG.isChecked()) {

                            setDefaultAddress(id, false);

                            LatteLogger.e("isChecked = true -  the id =",id+"");

                        }else{
                            entity.getFields().clear();

                            setDefaultAddress(id,true);

                                LatteLogger.e("isChecked = false - the id =", id +"");

                        }
                    }
                });

                nameText.setText(name);
                phoneText.setText(phone);
                addressText.setText(address);
                break;
            default:
                break;
        }
    }

    private void setDefaultAddress(int id,boolean isDefault) {
        RestClient.builder()
                .url("setAddressToBeDefault")
                .loader(mContext)
                .params("uuid", AccountManager.getSignUUID())
                .params("addressId",id)
                .params("default", isDefault)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        final List<MultipleItemEntity> defaultData = new AddressDataConverter().setJsonData(response).convert();
                        mData.clear();
                        mData.addAll(defaultData);
                        notifyDataSetChanged();
                    }
                })
                .build()
                .post();
    }

}
